#include"headfile.h"

int modify(const char * str, const char * id)
{
    if(0 == strcmp("密码",str))
        return _modify_password(str, id);
    else
        return _modify_information(str,id);
}

int _modify_password(const char * str, const char * idx)
{
    FILE * fp = fopen("./document/user.txt","r+");
    member student;
    int newLine = 0, oldLine = 0;
    while(3 == fscanf(fp,"%d%s%s",&student.id,student.name,student.password))
    {
        oldLine += (newLine == 0) ? 0 : (3 * N + 1);
        newLine = ftell(fp);
        if(student.id == atoi(idx))
        {
            int n;
            printf("请输入您的密码 输入返回则返回到上一步:");
            if((n = seconfEntry(passwd_login,1)) == 0)
            {
                fclose(fp);
                return 0;
            }
pass:       printf("\n请输入您要修改的密码 输入返回则返回到上一步:");
            memset(student.password,0,N);
            system("stty -echo");
            scanf("%s",student.password);
            system("stty echo");
            if(0 == strcmp("返回",student.password))
            {
                fclose(fp);
                return 0;
            }
            printf("\n请再次输入您要修改的密码 输入返回则返回到上一步:");
            if(0 == (n = seconfEntry(student.password,0))) 
                goto pass;
            printf("\n");
            break;
        }
    }
    fseek(fp, oldLine, SEEK_SET);
    strcpy(passwd_login,student.password);
    fprintf(fp,"%20d%20s%20s",student.id,student.name,student.password);

    fclose(fp);
}

int _modify_information(const char * str, const char * idx)
{
    FILE * fp = fopen("./document/student.txt","r+");
    member student;
    int newLine = 0, oldLine = 0;
    int id = atoi(idx); //将输入的字符型idx变成整型用于判断
    bool flag = false;
    if(0 == idx)
        flag = true;
    while(4 == fscanf(fp,"%s%s%d%d",student.name,student.sex,&student.age,&student.id))
    {
        oldLine += (newLine == 0) ? 0 : (4 * N + 1);
        newLine = ftell(fp);
        if(student.id == id || true == flag)
        {
            flag = false;
            if(0 == strcmp("姓名",str))
            {
                printf("请输入你要重新输入的名字 输入返回则返回到上一步:");
                scanf("%s%*c",student.name);
                if(0 == strcmp("返回",student.name))
                {
                    fclose(fp);
                    return 0;
                }

                //改用户文件夹里的姓名
                {
                    FILE * fp_user = fopen("./document/user.txt","r+");
                    member mem;
                    int newLine_user = 0,
                        oldLine_user = 0;
                    while(3 == fscanf(fp_user,"%d%s%s",&mem.id,mem.name,mem.password))
                    {
                        oldLine_user += (newLine_user == 0) ? 0 : 61;
                        newLine_user = ftell(fp_user);
                        if(mem.id == id)
                        {
                            strcpy(mem.name,student.name);
                            fseek(fp_user, oldLine_user, SEEK_SET);
                            fprintf(fp_user,"%20d%20s%20s",mem.id,mem.name,mem.password);
                            break;
                        }
                    }
                    fclose(fp_user);
                }

                if(0 == strcmp(idx,id_login)) //改界面显示的用户名
                    strcpy(name_login,student.name);
                if(student.id == id)
                {
                    fseek(fp, oldLine, SEEK_SET);
                    fprintf(fp,"%20s%20s%20d%20d",student.name,student.sex,student.age,student.id);
                }

                break;
            }
            else if(0 == strcmp("性别",str))
            {
                printf("请输入你要重新输入的性别 输入返回则返回到上一步:");
                while(1 == scanf("%s%*c",student.sex))
                {
                    if(0 == strcmp("返回",student.sex))
                    {
                        fclose(fp);
                        return 0;
                    }
                    if(0 == strcmp("男",student.sex) || 0 == strcmp("女",student.sex))
                        break;
                    printf("您输入的性别有误,请重新进行输入:");
                }

                fseek(fp, oldLine, SEEK_SET);
                fprintf(fp,"%20s%20s%20d%20d",student.name,student.sex,student.age,student.id);
                break;
            }
            else if(0 == strcmp("年龄",str))
            {
                printf("请输入你要重新输入的年龄 输-1返回:");
                while(0 == scanf("%d",&student.age))
                {
                    printf("你输入的年龄有误,请重新进行输入 输-1返回:");
                    char c;
                    while((c = getchar()) != '\n');
                }
                if(-1 == student.age) 
                {
                    fclose(fp);
                    return 0;
                }
                fseek(fp, oldLine, SEEK_SET);
                fprintf(fp,"%20s%20s%20d%20d",student.name,student.sex,student.age,student.id);
                break;
            }
            else
            {
                printf("你输入的参数有问题,");
                fclose(fp);
                return 0;
            }
        }
    }
    fclose(fp);
    return 0;
}